<?php
/*
	author yanxf
*/
class XML
{
	public $error;

	public function create($array)
	{
		$body = self::arrayToXML($array);
		$body = str_replace('</xml>','',str_replace('<xml>','',$body));
		$xml = '<xml>'.$body.'</xml>';
		return $xml;
	}

	public function arrayToXML($array)
	{
		$xml = '';
		if(!is_array($array))
		{
			return false;
		}
		foreach($array as $key=>$value)
		{
			if(is_array($value))
				$xml = $xml.'<'.$key.'>'.self::arrayToXML($value).'</'.$key.'>';
			else{
				if(is_numeric($value))
					$xml = $xml.'<'.$key.'>'.$value.'</'.$key.'>';
				else{
					if($this->xml_parser($value))
						$xml = $xml.'<'.$key.'>'.$value.'</'.$key.'>';
					else
						$xml = $xml.'<'.$key.'><![CDATA['.$value.']]></'.$key.'>';
				}
			}
		}
		return $xml;
	
	}
	
	public function xml_parser($str)
	{
		$xml_parser = xml_parser_create();
		if(!xml_parse($xml_parser,$str,true))
		{
			xml_parser_free($xml_parser);
			return false;
		}else{
			xml_parser_free($xml_parser);
			return true;
			//return (json_decode(json_encode(simplexml_load_string($str)),true));
		}
	}
}
